//
//  HUD+Snackbar.swift
//  BWHUDKit
//
//  Created by zhuxuhong on 2022/3/21.
//

import UIKit
import BWToolKit


extension HUD {
    public static func snackbar(
        customView: UIView,
        position: ShowPosition = .top,
        duration: TimeInterval = .infinity
    ) {
        show(.snackbar(.init(icon: nil, text: nil, action: nil, postition: position, customView: customView, duration: duration, maskColor: .clear)))
    }
    
    public static func snackbar(
        icon: Snack.Icon?,
        text: String?,
        action: Snack.Action? = nil
    ) {
        show(.snackbar(.init(icon: icon, text: text, action: action, postition: .top, customView: nil, duration: 0, maskColor: .clear)))
    }
    
    public static func snackbar(
        icon: Snack.Icon?,
        text: String?,
        action: Snack.Action? = nil,
        poistion: ShowPosition = .top,
        duration: TimeInterval = 0
    ) {
        show(.snackbar(.init(icon: icon, text: text, action: action, postition: poistion, customView: nil, duration: duration, maskColor: .clear)))
    }
}


extension HUD {
    public class Snack: HUD.Data {
        var action: Action?
        
        public enum Icon {
            case success, error, warning
            case custom(_ image: UIImage?)
            
            static let iconCheckmark = UIImage.checkmark(iconSize: .init(width: 24, height: 12), imageSize: .bw_with(both: 24), lineWidth: 1)
            static let iconXmark = UIImage.xmark(iconSize: .bw_with(both: 14), imageSize: .bw_with(both: 24), lineWidth: 1)
            static let iconExcmark = UIImage.excmark(iconSize: .bw_with(both: 16), imageSize: .bw_with(both: 24), lineWidth: 1)
            
            public var image: UIImage? {
                switch self {
                case .success:
                    return Self.iconCheckmark
                case .error:
                    return Self.iconXmark
                case .warning:
                    return Self.iconExcmark
                case .custom(let image):
                    return image
                }
            }
        }
        
        public init(
            icon: Icon? = nil,
            text: String? = nil,
            action: Action? = nil,
            postition: ShowPosition = .top,
            customView: UIView? = nil,
            duration: TimeInterval = .infinity,
            maskColor: UIColor = .clear
        ) {
            self.action = action
            var d = (duration>0 || duration.isInfinite) ? duration : Duration.fit(text: text)
            if action?.title != nil || action?.icon != nil {
                d = .infinity
            }
            super.init(icon: icon?.image, text: text, customView: customView, duration: d, maskColor: maskColor, blockable: false, position: postition)
        }
        
        public struct Action {
            var title: String?
            var icon: UIImage?
            var handler: BlockVoid?
            public init(title: String?, icon: UIImage?, handler: BlockVoid?) {
                self.title = title
                self.icon = icon
                self.handler = handler
            }
        }
    }
}
